package com.controller.ht;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.ajax.AjaxRes;
import com.common.mybatis.Page;
import com.common.utils.base.UuidUtil;
import com.controller.base.BaseController;
import com.entity.AppendixRelationship;
import com.entity.Information;
import com.entity.NewsColumn;
import com.service.AppendixRelationshipService;
import com.service.informationService;
import com.service.newsColumnService;
import com.service.newsColumnServiceImp;

@Controller
@RequestMapping("/information/")
public class informationController extends BaseController<Object>{

	@Autowired
	private informationService infoservice;
	@Autowired
	private AppendixRelationshipService apprpservice;
//	@Autowired
//	private newsColumnService ncservice;
	/**
	 * 查询所有
	 * @return
	 */
	@RequestMapping("/findAllUI")
	@ResponseBody
	public AjaxRes findAllUI(Information info){
		AjaxRes res = new AjaxRes();
		List<Information> infos = infoservice.find(info);
		res.setSucceed(infos,"数据获取成功");
		return res;
	}
	@RequestMapping("/showInfo")
	@ResponseBody
	public AjaxRes showInfo(Information info,Page<Information> page)throws Exception{
		AjaxRes res=new AjaxRes();
		Page<Information> indPages =infoservice.showInfo(new Information(), page,info.getColumnNumber());
		if(indPages.getResults()==null){
			res.setResMsg("数据查询为0");
		}else{
			// 获得SimpleDateFormat类
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			for (int i = 0; i < indPages.getResults().size(); i++) {
				//转换时间格式
				indPages.getResults().get(i).setReleaseTimeToString(sf.format(indPages.getResults().get(i).getReleaseTime()));
			}
			res.setSucceed(indPages, "数据获取成功");
		}
		System.out.println(res);
		return res;
	}
	/**
	 * 分页查询
	 * @return
	 */
	@RequestMapping("/findByPage")
	@ResponseBody
	public AjaxRes findByPage(Information info,Page<Information> p){
		System.out.println("查询新闻====================================");
		AjaxRes res = new AjaxRes();
		Page<Information> pageUis = infoservice.findByPage(info, p);
		// 获得SimpleDateFormat类
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		for (int i = 0; i < pageUis.getResults().size(); i++) {
			//转换时间格式
			pageUis.getResults().get(i).setReleaseTimeToString(sf.format(pageUis.getResults().get(i).getReleaseTime()));
		}
		System.out.println(pageUis.getResults().get(0).toString());
		res.setSucceed(pageUis, "数据获取成功");
		return res;
	}
	/**
	 * 修改新闻栏目并返回修改结果
	 * @return
	 */
	@RequestMapping("/update")
	@ResponseBody
	public AjaxRes update(Information info,String apprpId){
		AjaxRes res=new AjaxRes();
		AppendixRelationship apprp = new AppendixRelationship(); 
		if(info.getId().equals("0")){
			info.setId(UuidUtil.get32UUID());
			info.setPublisher("admin");
			info.setReleaseTime(new Date());
			System.out.println(info.toString());
			if(!apprpId.equals("")){
				//添加附件关系
				apprp.setId(apprpId);
				List<AppendixRelationship> apprpUis = apprpservice.find(apprp);
				apprpUis.get(0).setRelationNo(info.getId());
				apprpUis.get(0).setRelationTName("information");
				apprpservice.update(apprpUis.get(0));
			}	
			//用于测试是否添加成功的变量
			Integer a =infoservice.insert(info);
			if(a==0){
				res.setResMsg("添加失败");
			}else{
				res.setResMsg("添加成功");
			}
		}else{
			if(!apprpId.equals("")){
				//添加附件关系
				apprp.setId(apprpId);
				List<AppendixRelationship> apprpUis = apprpservice.find(apprp);
				apprpUis.get(0).setRelationNo(info.getId());
				apprpUis.get(0).setRelationTName("information");
				apprpservice.update(apprpUis.get(0));
			}
			//用于测试是否修改成功的变量
			Integer a =infoservice.update(info);
			if(a==0){
				res.setResMsg("修改失败");
			}else{
				res.setResMsg("修改成功");
			}
		}
		return res;
	}
	@RequestMapping("/delect")
	@ResponseBody
	public AjaxRes delect(Information info){
		AjaxRes res = new AjaxRes();
		//用于测试是否添加成功的变量
		Integer delRes = infoservice.delete(info);
		if(delRes==0){
			res.setResMsg("删除失败");
		}else{
			res.setResMsg("删除成功");
		}
		return res;
	}

	//批量删除
	@RequestMapping("/deleteBatch")
	@ResponseBody
	public AjaxRes deleteBatch(String uiCheck){
		AjaxRes res=new AjaxRes(); 
		List infoList=new ArrayList();
		String[] getArr = uiCheck.split(",");
		for(int i=0;i<getArr.length;i++){
			if(i!=0){
				Information info = new Information();
				info.setId(getArr[i]);
				infoList.add(info);
			}
		}
		Integer a = infoservice.deleteBatch(infoList);
		if(a==0){
			res.setResMsg("批量删除失败");
		}else{
			res.setResMsg("批量删除成功");
		}
		return res;
	}
	@RequestMapping("/informationInfo")
	public String informationInfo(Model model,String id){
		Information info = new Information();
		info.setId(id);
		if(!id.equals("0")){
			List<Information> infoList = infoservice.find(info);
			if(infoList.size()>0){
				info = infoList.get(0);
			}
		}
		model.addAttribute("info", info);
		return "pages/informationInfo";
	}
}
